package com.szm.userserver.webui;

import com.szm.userserver.entity.User;
import com.szm.userserver.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController {

    @Autowired
    //@Qualifier("a")
    private UserMapper userMapper;
    //private User user;

    @Autowired
    private DiscoveryClient discoveryClient;


    @Bean
    public User a(){
        return new User();
    }

    @Bean
    public User b(){
        return new User();
    }


    @GetMapping("/test")
    @ResponseBody
    public User test() {
        User user =  userMapper.getUserById(9);
        user.setServerAddress(getLocalInstanceInfo());
        //user.setId(100L);
        //user.setUsername("test");
        return user;
    }

    @GetMapping("/helloworld")
    public String helloWorld(@RequestParam("id") long id, Model model){
        User user=userMapper.getUserById(id);
        model.addAttribute("user",user);
        return "/helloworld";
    }

    private String getLocalInstanceInfo() {
        ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
        return serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort();
    }
}
